package cn.com.infohold.smartcity.activity.Fabaofang;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.adapter.Fabaofang_Adapter;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.FaBaoFangBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RecyclerViewDivider;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;
import library.utils.DisplayUtil;
import library.utils.Utils;

/**
 * Created by Monsoon on 2017/11/7.
 */
public class FaBaoFang_Activity extends ParentActivity{

    @ViewInject(R.id.tv_title)
    TextView tv_title;
    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.et_search)
    EditText et_search;
    @ViewInject(R.id.ib_search)
    ImageButton ib_search;
    @ViewInject(R.id.mRecylerview)
    RecyclerView mRecylerview;
    @ViewInject(R.id.btn_add)
    Button btn_add;

    Fabaofang_Adapter mAdapter;

    private List<FaBaoFangBean> mList = new ArrayList<>();

    @Override
    protected Object inflateView() {
        return R.layout.activity_search_list;
    }

    @Override
    protected void initView(View anchor) {

        btn_add.setVisibility(View.VISIBLE);
        tv_title.setText("发包方管理");

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransitionFade();
            }
        });

        ib_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startActivity(FaBaoFang_Activity.this, FaBaoFangAdd_Activity.class);
                overridePendingTransitionFade();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initRV(){
        int dividerHeight = DisplayUtil.dp2px(0);
        mRecylerview.setLayoutManager(new LinearLayoutManager(this));
        mRecylerview.addItemDecoration(new RecyclerViewDivider(dividerHeight));
        mAdapter = new Fabaofang_Adapter(this,mList);
        mRecylerview.setAdapter(mAdapter);
    }

    private void initData(){
        SDK.api().getFBList(1,15,"","","").postEvent(RequestCode.REQUEST_CODE_GET_FB_LIST);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent){

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_GET_FB_LIST) {
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            return;
        } else {
            switch (requestCode){
                case RequestCode.REQUEST_CODE_GET_FB_LIST:
                    Type type = new TypeToken<LandResult<ListResult<FaBaoFangBean>>>(){}.getType();
                    LandResult<ListResult<FaBaoFangBean>> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    if (result.getStatus().equals("1")){
                        List<FaBaoFangBean> list = result.getResult().getList();
                        mList.clear();
                        mList.addAll(list);
                        initRV();
                    }

                    break;
                default:
                    break;
            }
        }
    }

}
